package com.itheima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.util.RequestContextUtil;
import com.itheima.common.vo.PageResultVo;
import com.itheima.wemedia.dto.WmMaterialPageRequestDto;
import com.itheima.wemedia.mapper.WmMaterialMapper;
import com.itheima.wemedia.pojo.WmMaterial;
import com.itheima.wemedia.service.WmMaterialService;
import org.springframework.stereotype.Service;

/**
 * @description <p>自媒体图文素材信息 业务实现</p>
 *
 * @version 1.0
 * @package com.itheima.wemedia.service.impl
 */
@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    /**
     * 分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public PageResultVo<WmMaterial> findPage(WmMaterialPageRequestDto dto) {
        //1. 分页参数设置
        IPage<WmMaterial> pageParam = new Page<WmMaterial>(dto.getPage(), dto.getSize());
        //2. 构建分页条件
        LambdaQueryWrapper<WmMaterial> queryWrapper = new LambdaQueryWrapper<>();
        //2.1 素材必须是当前用户上传的，即user_id=登录用户id
        queryWrapper.eq(WmMaterial::getUserId, RequestContextUtil.getLoginUserId());
        //2.2 是否 收藏
        queryWrapper.eq(null!=dto.getIsCollection()&&dto.getIsCollection().intValue() != 0, WmMaterial::getIsCollection, dto.getIsCollection());
        //3. 分页查询
        IPage<WmMaterial> pageInfo = page(pageParam, queryWrapper);
        //4. 构建分页结果并返回
        return PageResultVo.pageResult(dto.getPage(),dto.getSize(),pageInfo.getTotal(), pageInfo.getRecords());
    }
}
